<?php
/**
 *
 * @Author nikm $
 *
 * @Date 2012-01-12 $
 */

if(!defined('FROM_INDEX')) exit('Invalid request');

require ROOT_PATH.'module/ajax/master.mod.php';

class moduleObj extends masterObj
{
	function __construct($request)
	{
		parent::__construct($request);
		
		$this->excute();
	}

	function excute()
	{
		switch($this->act)
		{
			case 'do_add':
				$this->doAdd();
				break;
			case 'do_delete':
				$this->doDelete();
				break;
			default:
				exit();
		}
	}

	private function doAdd()
	{
		$request['movie_id'] = isset($this->request['mid']) ? (int)$this->request['mid'] : '';
		$refers = isset($this->request['refer']) ? trim($this->request['refer']) : '';
		
		$refers = explode("\n", $refers);
		
		$movie = get_movie_info($request['movie_id']);

		if(!isset($movie['id']) || !$movie['id'])
		{
			$this->message("影片不存在");
		}

		$result = false;
		foreach($refers as $refer)
		{
			if(strlen($refer) > 999)
			{
				$this->message("内容超长");
			}
		
			if(trim($refer) != '')
			{
				$request['refer'] = trim($refer);
				$result = add_movie_refer($request);
			}
		}
		
		if($result)
		{
			$this->message("添加成功");
		}
		else
		{
			$this->message("添加失败");
		}
	}
	
	private function doDelete()
	{
		$mid = isset($this->request['mid']) ? (int)$this->request['mid'] : '';
		$ids = isset($this->request['ids']) ? trim($this->request['ids']) : '';
		
		if(check_array($ids))
		{
			$ids = implode(',', $ids);
		}
		
		$redirect = "admin.php?m=movie&a=modify&id={$mid}";
		
		$message = '';
		if(!$ids)
		{
			//$this->messager("请指定ID", $redirect);
			$message = '请指定ID';
		}
		else
		{
			$request['ids'] = $ids;
			
			$result = delete_movie_refer($request);
			
			if($result)
			{
				//$this->messager("删除成功", $redirect);
				$message = '删除成功';
			}
			else
			{
				//$this->messager("删除失败", $redirect);
				$message = '删除失败';
			}
		}
		
		//echo $message;
		echo "<script type='text/javascript'>";
		echo "alert('{$message}');";
		echo "window.location.href='".BASE_URL."$redirect';";
		echo "</script>";
	}
}